using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using RecruitmentSystem.Business;
using RecruitmentSystem.Common;
using System.Collections.Generic;

public partial class SystemManagement_NewsCategoryManagement : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindTree();
        }
    }

    private void BindTree()
    {
        List<Category> categoryList = Application["AllCategory"] as List<Category>;
        if (categoryList == null || categoryList.Count <= 0)
        {
            NewsManager nm = new NewsManager();
            categoryList = nm.GetAllCategory(null);
            Application["AllCategory"] = categoryList;
        }
        foreach (Category ctg in categoryList)
        {
            if (ctg.ParentId == 0)
            {
                TreeNode rootCategory = new TreeNode();
                rootCategory.Text = ctg.Name;
                rootCategory.Value = ctg.Id.ToString();
                rootCategory.NavigateUrl = "CategoryOperate.aspx?cid=" + ctg.Id + "&cname=" + ctg.Name + "&level=" + ctg.Level;
                rootCategory.Target = "ifrOperate";
                BindSecondTree(rootCategory);
                treeCategory.Nodes.Add(rootCategory);
            }
        }
        
    }

    private void BindSecondTree(TreeNode tn)
    { 
        List<Category> categoryList = Application["AllCategory"] as List<Category>;
        if (categoryList == null || categoryList.Count <= 0)
        {
            NewsManager nm = new NewsManager();
            categoryList = nm.GetAllCategory(null);
            Application["AllCategory"] = categoryList;
        }
        foreach (Category ctg in categoryList)
        {
            if (ctg.Level == 2)
            {
                if (ctg.ParentId.ToString() == tn.Value)
                {
                    TreeNode secondNode = new TreeNode();
                    secondNode.Text = ctg.Name;
                    secondNode.Value = ctg.Id.ToString();
                    secondNode.NavigateUrl = "CategoryOperate.aspx?cid=" + ctg.Id + "&cname=" + ctg.Name + "&level=" + ctg.Level;
                    secondNode.Target = "ifrOperate";
                    tn.ChildNodes.Add(secondNode);
                }
            }
        }
    }

}
